# "执行前删除Bridge6/Linux目录"
# "执行前删除Bridge6{analysis,BridgeAnalysis,BridgeTest,ChemDLL}/build目录"
# 执行前可能需要执行以下指令：
# dos2unix *.sh
# dos2unix libs/*.sh
# chmod +x *.sh
# chmod +x libs/*.sh

export SOLUTION_DIR="/root/work/bridge6"

### : << 'COMMENT'
# 脚本带参数-r则清理构建目录
has_r_flag=0
for arg in "$@"; do
    if [ "$arg" = "-r" ]; then
        has_r_flag=1
        break
    fi
done
if [ $has_r_flag -eq 1 ]; then
# 清理构建目录
    target_dirs=(
        "$SOLUTION_DIR/analysis/build/linux_x64_debug"
        "$SOLUTION_DIR/analysis/build/linux_x64_release"
        "$SOLUTION_DIR/BridgeAnalysis/build/linux_x64_debug"
        "$SOLUTION_DIR/BridgeAnalysis/build/linux_x64_release"
        "$SOLUTION_DIR/ChemDLL/build/linux_x64_debug"
        "$SOLUTION_DIR/ChemDLL/build/linux_x64_release"
        "$SOLUTION_DIR/BridgeTest/build/linux_x64_debug"
        "$SOLUTION_DIR/BridgeTest/build/linux_x64_release"
        "$SOLUTION_DIR/LogService/build/linux_x64_debug"
        "$SOLUTION_DIR/LogService/build/linux_x64_release"
        "$SOLUTION_DIR/Linux/Debug"
        "$SOLUTION_DIR/Linux/Release"
        "$SOLUTION_DIR/libs/redis-plus-plus-master/build_linux_r"
        "$SOLUTION_DIR/libs/redis-plus-plus-master/build_linux_d"
        "$SOLUTION_DIR/libs/hiredis-master/build_linux_r"
        "$SOLUTION_DIR/libs/hiredis-master/build_linux_d"
        "$SOLUTION_DIR/libs/mysql-connector-c++-8.4.0-src/build_linux_r"
        "$SOLUTION_DIR/libs/mysql-connector-c++-8.4.0-src/build_linux_d"        
        "$SOLUTION_DIR/libs/googletest-1.17.0/build_linux_r"
        "$SOLUTION_DIR/libs/googletest-1.17.0/build_linux_d"
    )
    for dir in "${target_dirs[@]}"; do
        if [ -d "$dir" ]; then
            echo "删除目录: $dir"
            rm -rf "$dir" || { echo "删除目录失败"; exit 1; }
        else
            echo "目录不存在，跳过: $dir"
        fi
    done
    exit 0
fi 

echo "1. 构建hiredis库..."
cd $SOLUTION_DIR/libs
./mk_1_hiredis.sh

echo "2. 构建redis++库..."
cd $SOLUTION_DIR/libs
./mk_2_redis++.sh

echo "3. 构建mysql-connector库..."
echo "跳过，通过 apt install -y libmysqlcppconn-dev 安装"

echo "4. 构建boost库..."
cd $SOLUTION_DIR/libs
./mk_4_boost.sh

echo "5. 构建googletest库..."
cd $SOLUTION_DIR/libs
./mk_5_googletest.sh

echo "6. 构建valgrind库..."
echo "跳过，通过 apt install valgrind 安装"

echo "7. 构建analysis/Release库..."
cd $SOLUTION_DIR/analysis
mkdir -p build/linux_x64_release
cd build/linux_x64_release
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "8. 构建analysis/Debug库..."
cd $SOLUTION_DIR/analysis
mkdir -p build/linux_x64_debug
cd build/linux_x64_debug
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "9. 构建BridgeAnalysis/Release库..."
cd $SOLUTION_DIR/BridgeAnalysis
mkdir -p build/linux_x64_release
cd build/linux_x64_release
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "10. 构建BridgeAnalysis/Debug库..."
cd $SOLUTION_DIR/BridgeAnalysis
mkdir -p build/linux_x64_debug
cd build/linux_x64_debug
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install
### COMMENT

echo "11. 构建ChemDLL/Release库..."
cd $SOLUTION_DIR/ChemDLL
mkdir -p build/linux_x64_release
cd build/linux_x64_release
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "12. 构建ChemDLL/Debug库..."
cd $SOLUTION_DIR/ChemDLL
mkdir -p build/linux_x64_debug
cd build/linux_x64_debug
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "13. 构建BridgeTest/Release执行程序..."
cd $SOLUTION_DIR/BridgeTest
mkdir -p build/linux_x64_release
cd build/linux_x64_release
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "14. 构建BridgeTest/Debug执行程序..."
cd $SOLUTION_DIR/BridgeTest
mkdir -p build/linux_x64_debug
cd build/linux_x64_debug
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "15. 构建LogServicce/Release执行程序..."
cd $SOLUTION_DIR/LogServicce
mkdir -p build/linux_x64_release
cd build/linux_x64_release
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "16. 构建LogServicce/Debug执行程序..."
cd $SOLUTION_DIR/LogServicce
mkdir -p build/linux_x64_debug
cd build/linux_x64_debug
cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
ninja -v -j$(nproc)
ninja install

echo "17. 测试BridgeTest/Release执行程序..."
cd $SOLUTION_DIR/Linux/Release
export LD_LIBRARY_PATH="$SOLUTION_DIR/Linux/Release"
ln -s ./BridgeTest-0.0.1 ./BridgeTest
./BridgeTest --gtest_filter=AnalysisTest2*
./BridgeTest --gtest_filter=ChemDLLTest1*

echo "18. 测试BridgeTest/Debug执行程序..."
cd $SOLUTION_DIR/Linux/Debug
export LD_LIBRARY_PATH="$SOLUTION_DIR/Linux/Debug"
ln -s ./BridgeTest-0.0.1 ./BridgeTest
./BridgeTest --gtest_filter=AnalysisTest2*
./BridgeTest --gtest_filter=ChemDLLTest1*